import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;

public class Segment 
{
	private Vector<SegmentPiece> pixels;
	private SegMomentsList moments;
	private int id;
	private LetterType letter;
	private int borders[];
	private int center[];
	private int weights[];
	private int diameters[];
	private int L;
	private int S;
	private int rectArea;
	private int maxDiameter;
	
		
	public void setRectArea(int rectArea) 
	{
		this.rectArea = rectArea;
	}

	public int getRectArea() {
		return rectArea;
	}

	public void setWeights(int[] weights) {
		this.weights = weights;
	}

	public SegMomentsList getMoments() {
		return moments;
	}

	public void setMoments(SegMomentsList moments) {
		this.moments = moments;
	}

	public int getL() {
		return L;
	}

	public void setL(int l) {
		L = l;
	}

	public int getS() {
		return S;
	}

	public void setS(int s) {
		S = s;
	}

	public int getMaxDiameter() {
		return maxDiameter;
	}

	public void setMaxDiameter(int maxDiamter) {
		this.maxDiameter = maxDiamter;
	}

	public int[] getCenter() {
		return center;
	}

	public int[] getWeights() {
		return weights;
	}

	public int[] getBorders() 
	{
		return borders;
	}

	public Segment(int id) 
	{
		this.id=id;
        setLetter(LetterType.ANOTHER);        
        pixels = new Vector<SegmentPiece>();
        borders = new int[4];
        center = new int[2];
        weights = new int[2];
        diameters = new int[2];
    }

    public void addItem(SegmentPiece sp) {
        pixels.add(sp);
    }

    public Vector<SegmentPiece> getPixels() 
    {
        return pixels;
    }
    
    public int getSize()
    {
    	return pixels.size();
    }

    public int getId() 
    {
        return id;
    }

	public void setLetter(LetterType letter) {
		this.letter = letter;
	}

	public LetterType getLetter() {
		return letter;
	}
	
	public int[] computeBordersAndDiameters()
	{
        int minX = 3000;
        int minY = 3000;
        int maxX = -1;
        int maxY = -1;
        
		for (Iterator<SegmentPiece> iterator = pixels.iterator(); iterator.hasNext();) 
		{	        
			SegmentPiece sp = iterator.next();
            if (sp.x < minX) 
                minX = sp.x;            
            if (sp.x > maxX) 
                maxX = sp.x;
            
            if (sp.y < minY) 
                minY = sp.y;            
            if (sp.y > maxY) 
                maxY = sp.y;            
        }
        
        borders[0] = minX;
        borders[1] = maxX;
        borders[2] = minY;
        borders[3] = maxY;
        
        diameters[0] = borders[1]-borders[0];
        diameters[1] = borders[3]-borders[2];
        
        maxDiameter = Math.max(diameters[0], diameters[1]);
        rectArea = diameters[0] * diameters[1];
        
        return borders;
	}
	
	public int[] computeCenters()
	{
        center[0] = (borders[0] + borders[1]) / 2; //X
        center[1] = (borders[2] + borders[3]) / 2; //Y
        
        return center;
	}

	@Override
	public String toString() 
	{
		String s = "Segment id = "+id+"\n"
				  +"letter: "+letter+"\n"
				  +"L= "+L+"\n"
				  +"S= "+S+"\n"
				  +"centers: "+Arrays.toString(center)+"\n"
		 		  +"weights: "+Arrays.toString(weights)+"\n"
		 		  +"borders: "+Arrays.toString(borders)+"\n"
				  +"MOMENTS: " + moments+"\n";
		return s;
	}
}
